package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.tools.TextWatcherExt;
import com.shouhuobao.station.R;
import com.shouhuobao.take.BaseActivityWithScroll;
import com.shouhuobao.take.model.ParcelBean;
import com.shouhuobao.take.utils.StringUtils;

/**
 * Created by joker on 2017/5/25. <br>
 * 包裹异常处理和包裹其他处理共用同一个类 通过from来判断是哪个界面 1为异常其他处理，显示edittext，其他的则不显示edittext
 */
public class ParcelExceptionActivity extends BaseActivityWithScroll implements View.OnClickListener {

    private TextView mPostCodeTV;// 快递单号
    private TextView mDeliveryOrderTV;// 提货单号
    private TextView mDeliveryPhoneTV;// 取货人手机号
    private TextView mDeliveryNameTV;// 取货人姓名
    private CheckBox[] mExceptions;
    private EditText mExceptionInput;

    private int from = 0;

    private int checkItem;

    private ParcelBean parcel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setScrollHeight(100);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_exception);
        super.findViewById();
        getAppTitle().setCommonTitle("异常处理");

        mPostCodeTV = (TextView) findViewById(R.id.postcode_TV);
        mDeliveryOrderTV = (TextView) findViewById(R.id.deliveryorderTV);
        mDeliveryPhoneTV = (TextView) findViewById(R.id.parcel_receiver_phone);
        mDeliveryNameTV = (TextView) findViewById(R.id.deliverynameTV);
        mExceptions = new CheckBox[3];
        mExceptions[0] = (CheckBox) findViewById(R.id.exception_item1);
        mExceptions[1] = (CheckBox) findViewById(R.id.exception_item2);
        mExceptions[2] = (CheckBox) findViewById(R.id.exception_item3);
        mExceptionInput = (EditText) findViewById(R.id.exceptionET);

        mExceptions[0].setOnClickListener(this);
        mExceptions[1].setOnClickListener(this);
        mExceptions[2].setOnClickListener(this);
        findViewById(R.id.submit).setOnClickListener(this);
        initView();
    }

    private void initView() {
        if (from == 1) {
            mExceptionInput.setVisibility(View.VISIBLE);
            mExceptionInput.addTextChangedListener(new TextWatcherExt() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() > 200) {
                        showToast("不能超过200个字");
                    }
                }
            });
        } else {
            mExceptionInput.setVisibility(View.GONE);
        }
        parcel = (ParcelBean) getIntent().getSerializableExtra("item");
        mPostCodeTV.setText(parcel.getExpressCompanyName() + " : " + StringUtils.splitNumString(2, parcel.getExpressNumber()));
        mDeliveryOrderTV.setText(getString(R.string.take_bag_name_desc) + "：" + parcel.getStorageCode());
        mDeliveryPhoneTV.setText(StringUtils.splitNumString(1, parcel.getReceiverPhone()));
        mDeliveryNameTV.setText("取件人姓名: " + parcel.getReceiverName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 点击提交按钮
            case R.id.submit:
                String content = mExceptions[checkItem].getText().toString();
                if (checkItem == 2) {
                    content = mExceptionInput.getText().toString().trim();
                }
                AppHttp.getInstance().takeParcelException(parcel.getId(), content);
                break;
            // 选择异常1
            case R.id.exception_item1:
                checkItem = 0;
                mExceptions[0].setChecked(true);
                mExceptions[1].setChecked(false);
                mExceptions[2].setChecked(false);
                mExceptionInput.setVisibility(View.GONE);
                break;
            // 选择异常2
            case R.id.exception_item2:
                checkItem = 1;
                mExceptions[0].setChecked(false);
                mExceptions[1].setChecked(true);
                mExceptions[2].setChecked(false);
                mExceptionInput.setVisibility(View.GONE);
                break;
            // 选择其他异常
            case R.id.exception_item3:
                checkItem = 2;
                mExceptions[0].setChecked(false);
                mExceptions[1].setChecked(false);
                mExceptions[2].setChecked(true);
                mExceptionInput.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.parcel_exception_submit:
                ReqResult<?> result = JSON.parser(msg.obj);
                if (checkLoginStatus(result)) {
                    showToast(result.getMessage());
                    setResult(Activity.RESULT_OK);
                    finish();
                    //                    Intent intent = new Intent();
                    //                    intent.setClass(this, MainActivity.class);
                    //                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    //                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //                    startActivity(intent);
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
